<template>
  <el-dialog title="分配" append-to-body :close-on-click-modal="false" @close="closeDialog()" v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属停车场" prop="parkId">
            <el-select v-model="dataForm.parkId" clearable class="m-2" placeholder="所属停车场" @change="getRoadId">
              <el-option v-for="item in parkNames" :key="item.id" :label="item.parkName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>

        <el-col :span="12">
          <el-form-item label="所属区域" prop="regionId">
            <el-select v-model="dataForm.regionId" clearable class="m-2" placeholder="所属区域" @change="getLotId">
              <el-option v-for="item in regionNames" :key="item.id" :label="item.regionName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="车位号" prop="lotId">
            <el-select v-model="dataForm.lotId" clearable class="m-2" placeholder="车位号">
              <el-option v-for="item in lotIdNames" :key="item.id" :label="item.lotNo" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/roadStop/lotdevice'
import { fetchList } from '@/api/roadStop/regionroad'
import { regionRoadAll, fetchListLot } from '@/api/roadStop/regionlotroad'

export default {
  name: 'lotdevice-Type',
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        sn: null
      },
      dataRule: {
        serviceCode: [
          { required: true, message: '服务code(适配不同商家地磁)不能为空', trigger: 'blur' }
        ],

        sn: [
          { required: true, message: '地磁编号不能为空', trigger: 'blur' }
        ],

        lotId: [
          { required: true, message: '泊位(车位id)编号不能为空', trigger: 'blur' }
        ],

        parkId: [
          { required: true, message: '车场id不能为空', trigger: 'blur' }
        ],

        type: [
          { required: true, message: '0未分配，1已分配不能为空', trigger: 'blur' }
        ],

        isOnLine: [
          { required: true, message: '是否在线不能为空', trigger: 'blur' }
        ],

      },
      parkNames: [],
      regionNames: [],
      lotIdNames: [],
      getParkIds: {
        isRoad: 1
      }
    }
  },
  created () {
    this.getDataListTwo()
  },
  methods: {
    getDataListTwo () {
      fetchList(this.getParkIds).then(response => {
        this.parkNames = response.data.data
      })
    },
    getDataListRoad () {
      regionRoadAll(this.getParkIds).then(response => {
        this.regionNames = response.data.data
      })
    },
    getDataLot () {
      fetchListLot(this.getParkIds).then(res => {
        this.lotIdNames = res.data.data
      })
    },
    getRoadId (val) {
      this.dataForm.regionId = undefined
      this.dataForm.lotId = undefined
      this.getParkIds.regionId = undefined
      this.getParkIds.parkId = val
      this.getDataListRoad()
    },
    getLotId (val) {
      this.dataForm.lotId = undefined
      this.getParkIds.parkId = undefined
      this.getParkIds.regionId = val
      this.getDataLot()
    },
    init (val) {
      this.visible = true;
      this.canSubmit = true;
      this.dataForm.sn = val.sn
      this.dataForm.id = val.id
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.parkId = val.parkId
        this.dataForm.regionId = val.regionId
        this.dataForm.lotId = val.lotId
        this.getParkIds.parkId = val.parkId
        this.getParkIds.regionId = val.regionId
        this.getDataListRoad()
        this.getDataLot()
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          putObj(this.dataForm).then(data => {
            this.$notify.success('分配成功')
            this.visible = false
            this.$emit('refreshDataList')
          }).catch(() => {
            this.canSubmit = true;
          });

        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
